<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <script type="module">
        import {consoleprint} from "./modules/first.js"

        consoleprint("定义generator");

        function* gen1() { //todo 这里有　*
            yield 1;
            yield 2;
            yield 3
        }

        // 手动
        let g1 = gen1();
        console.log(g1.next()); // {value: 1, done: false}
        console.log(g1.next());
        console.log(g1.next());

        let g2 = gen1();
        for (let val of g2) {
            console.log(val)
        }
        // todo 解构
        let [a, ...b] = gen1(); //1 [2, 3]
        console.log(a, b);

        consoleprint("第一步返回的结果发送给第二步")
        // axios
        let user = "taojun319";
        let requestGiteeuser = (username) => {
            return axios.get(`https://gitee.com/api/v5/users/${username}`) // 是promise对象
        };

        function* genaxios() {
            let val = yield  user;
            yield requestGiteeuser(val)
        }

        let gaxios = genaxios();
        let username = gaxios.next().value;
        // console.log(username)

        gaxios.next(username).value.then(result => {
            let {data, status} = result
            if (status == 200) {
                console.log(data)
            } else {
                throw new Error("status code not 200") // 创建错误 让后面捕获
            }
        }).catch(err => {
            console.log(err)
        });



    </script>
</head>
<body>

</body>
</html>